<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:销售订单
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class SalesordersAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
		//$map['wahname|egname|wahcode|wahaddr'] = array('like',"%".$_POST['selectname']."%");
        
	}
   //页面加载前生产单据编号
    function _before_index(){
        $newCode = $this->getSerialNum('KD','Custorders');
        $this->assign ( 'newCode', $newCode );
        $this->assign ( 'dtime', date('Y-m-d') );
    }
    
    function addOrders(){
        if($_POST['ordernumber']){$mainData['ordernumber'] = $_POST['ordernumber'];}else{$this->error('单据编号不能为空。');exit;}
        if($_POST['custid_id']){$mainData['custid'] = $_POST['custid_id'];}else{$this->error('订购单位不能为空。');exit;}
        $mainData['custorder'] = $_POST['custorder'];       
        if($_POST['orderman']){$mainData['orderman'] = $_POST['orderman'];}else{$this->error('下单人不能为空。');exit;}
        if($_POST['ordertime']){$mainData['ordertime'] = strtotime($_POST['ordertime']);}else{$this->error('下单时间不能为空。');exit;}        
        if($_POST['total_nums']){$mainData['ordernums'] = $_POST['total_nums'];}else{$this->error('累计单据数量为0。');exit;}
        if($_POST['deliverytime']){$mainData['deliverytime'] = strtotime($_POST['deliverytime']);}else{$this->error('截止交期不能为空');exit;}
        $mainData['createuser'] = $_SESSION [C( 'USER_AUTH_KEY')];
        $mainData['createtime'] = strtotime(date('Y-m-d H:i:s'));
        $mainData['updatetime'] = strtotime(date('Y-m-d H:i:s'));       
        $mainData['status'] = 0;//设置为未结案
        $mainData['remarks'] = $_POST['remarks'];   
        
        //$a = $items['orgNumber'];
        
        
        $aProidData = $_POST['items_proid'];
        //$this->error($aProidData);exit;
        $countRow = count($aProidData);
        if($countRow==0){$this->error('单据没有添加任何商品信息。');exit;}
        $aCountData = $_POST['items_ordernums'];
        $aMarksData = $_POST['items_remark'];
        
                
        
              
        $saveModel = D('Custorders');        
        $map['ordernumber'] = $_POST['ordernumber'];
        if(($saveModel->where($map)->count('id'))>0){
            $this->error('该单据已提交过,请勿重复提交单据。');
        }else{
            if($saveModel->add($mainData)){
                $detailModel = D('Orderdetails');      
                    //循环本单据每笔单据信息写入数据库
                    for($i=0;$i<$countRow;$i++){
                        $sucsCount = 0;
                        unset($detailData);
                        $detailData['ordersort']   = $i+1;
                        $detailData['ordernumber'] = $_POST['ordernumber'];
                        $detailData['prodid']      = $aProidData[$i];
                        //$detailData['prodcode']    = 0;
                        $detailData['ordernums']   = $aCountData[$i];
                        //$detailData['orderprice']  = 0;
                        //$detailData['discounts']   = 0;
                        //$detailData['totalamount'] = 0;
                        $detailData['remarks']     = $aMarksData[$i];
                        $detailData['iotype']     = 0;//1为入库2为出库
                        if($detailModel->add($detailData)){
                        $sucsCount = $sucsCount+1;
                        }
                    }                    
                if($sucsCount = $countRow){ 
                   $this->assign ( "jumpUrl", $this->getReturnUrl());
                   $this->success('单据[ '.$_POST['ordernumber'].' ]已提交,请至[业务单据池]处理单据状态。'); 
                }else{
                   $saveModel->where($map)->delete();
                   $detailModel->where($map)->delete();
                   $this->error('单据[ '.$_POST['ordernumber'].' ]提交失敗,详细信息写入失败。');                   
                }                
            }else{
                $this->error('单据保存失败。');
            }
        }      
        //$this->success($a);
    }

    
 
}
?>